package com.yanqu.road.dao.impl.activity.snowgarden;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.snowgarden.UserSnowGardenVisitRecord;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserSnowGardenVisitRecordDaoImpl extends TempDataObjectDao<UserSnowGardenVisitRecord> {

    public static String getUserSnowGardenVisitRecordSql = "select * from `t_u_activity_snow_garden_record` where `activityId`=? and `userId`=?";
    public List<UserSnowGardenVisitRecord> getUserSnowGardenVisitRecord(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<UserSnowGardenVisitRecord> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserSnowGardenVisitRecordSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserSnowGardenVisitRecord data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String updateUserSnowGardenVisitRecordSql = "update `t_u_activity_snow_garden_record` set period=?,visitType=?,objectId=?,addScore=?,crossBack=? where activityId=? and userId=? and targetUserId=? and visitTime=?";
    @Override
    protected boolean updateData(UserSnowGardenVisitRecord userSnowGardenVisitRecord) {
        userSnowGardenVisitRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getPeriod()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getVisitType()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getObjectId()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getAddScore()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getCrossBack()));

        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRecord.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRecord.getVisitTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRecord.getTargetUserId()));
        if (getDb().executeNoneQuery(updateUserSnowGardenVisitRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserSnowGardenVisitRecordSql = "insert into `t_u_activity_snow_garden_record`(activityId,userId,period,targetUserId,visitType,objectId,addScore,visitTime,crossBack) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserSnowGardenVisitRecord userSnowGardenVisitRecord) {
        userSnowGardenVisitRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRecord.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getPeriod()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRecord.getTargetUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getVisitType()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getObjectId()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getAddScore()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRecord.getVisitTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getCrossBack()));
        if (getDb().executeNoneQuery(insertUserSnowGardenVisitRecordSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserSnowGardenVisitRecord parseData(ResultSet rs) throws SQLException {
        UserSnowGardenVisitRecord userSnowGardenVisitRecord = new UserSnowGardenVisitRecord();
        userSnowGardenVisitRecord.setActivityId(rs.getInt("activityId"));
        userSnowGardenVisitRecord.setUserId(rs.getLong("userId"));
        userSnowGardenVisitRecord.setPeriod(rs.getInt("period"));
        userSnowGardenVisitRecord.setTargetUserId(rs.getLong("targetUserId"));
        userSnowGardenVisitRecord.setVisitType(rs.getInt("visitType"));
        userSnowGardenVisitRecord.setObjectId(rs.getInt("objectId"));
        userSnowGardenVisitRecord.setAddScore(rs.getInt("addScore"));
        userSnowGardenVisitRecord.setVisitTime(rs.getLong("visitTime"));
        userSnowGardenVisitRecord.setCrossBack(rs.getInt("crossBack"));

        return userSnowGardenVisitRecord;
    }

    public boolean batchAdd(List<UserSnowGardenVisitRecord> tempDatas) {
        boolean result = false;
        if (tempDatas.size() == 0) {
            return result;
        }

        // 设置写时间
        for (UserSnowGardenVisitRecord data : tempDatas) {
            data.setBeginWriteTime();
        }

        getDb().setConnName(DbNameStrategy.getUserDb());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (UserSnowGardenVisitRecord userSnowGardenVisitRecord : tempDatas) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRecord.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getPeriod()));
            para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRecord.getTargetUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getVisitType()));
            para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getObjectId()));
            para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRecord.getAddScore()));
            para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRecord.getVisitTime()));
            mapList.add(para);
        }

        if (getDb().executeBatchQuery(insertUserSnowGardenVisitRecordSql, mapList) > -1) {
            // 设置写入状态
            for (UserSnowGardenVisitRecord data : tempDatas) {
                data.setNoneOption();
            }
            result = true;
        }
        return result;
    }
}
